package com.yst.metho.web.application.team;

import nextapp.echo2.app.Alignment;
import nextapp.echo2.app.Column;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.Grid;
import nextapp.echo2.app.Insets;
import nextapp.echo2.app.Label;
import nextapp.echo2.app.Row;
import nextapp.echo2.app.Table;
import nextapp.echo2.app.TextField;
import nextapp.echo2.app.layout.ColumnLayoutData;

import com.yst.metho.web.application.components.ListingViewPane;
import com.yst.metho.web.application.components.table.SortableTableHeaderCellRenderer;
import com.yst.metho.web.application.team.actions.AddRole;
import com.yst.metho.web.application.team.actions.DeleteRole;
import com.yst.metho.web.application.team.renderer.TeamRoleTableCellRenderer;
import com.yst.suivi.data.team.TeamDto;

public class TeamPane extends Column
{
	private TextField mNom;

	private String mId = null;

	private TeamDto mTeamDto;

	public TeamPane(TeamDto teamDto)
	{
		super();
		mTeamDto = teamDto;

		setCellSpacing(new Extent(10));
		setInsets(new Insets(10));
		Row row = new Row();
		Grid headerGrid = new Grid();
		headerGrid.setInsets(new Insets(0, 2));
		// add(headerGrid);
		Label label;
		label = new Label("Nom");
		// headerGrid.add(label);
		row.add(label);
		mNom = new TextField();
		mNom.setStyleName("TextField");
		// headerGrid.add(mNom);
		row.add(mNom);
		add(row);

		row = new Row();
		ColumnLayoutData layoutData = new ColumnLayoutData();
		layoutData.setHeight(new Extent(70, Extent.PX));
		row.setLayoutData(layoutData);
		Table table = new Table(2, 5);
		// headerGrid.add(table);
		row.add(table);
		// RowLayoutData layoutData = new RowLayoutData();
		// layoutData.setWidth(new Extent(100, Extent.PERCENT));
		// table.setLayoutData(layoutData);
		table.setSelectionEnabled(true);
		table.setRolloverEnabled(true);
		table.setStyleName(ListingViewPane.LISTING_VIEW_TABLE_STYLE);
		table.setActionCommand(ListingViewPane.LISTING_VIEW_TABLE_ACTION_COMMAND);
		table.setWidth(new Extent(350, Extent.PX));
		TeamRoleListModel model = new TeamRoleListModel(mTeamDto);
		table.setModel(model);
		table.setDefaultHeaderRenderer(new SortableTableHeaderCellRenderer());
		table.setDefaultRenderer(Object.class, new TeamRoleTableCellRenderer());
		add(row);

		row = new Row();
		row.setAlignment(Alignment.ALIGN_LEFT);
		AddRole add = new AddRole(mTeamDto, model);
		row.add(add);
		DeleteRole deleteRole = new DeleteRole(teamDto, model);
		table.getSelectionModel().addChangeListener(deleteRole);
		row.add(deleteRole);
		add(row);

		if (null != teamDto)
		{
			mId = teamDto.getId();
			mNom.setText(teamDto.getNom());
		}
	}

	public TeamPane()
	{
		this(new TeamDto());
	}

	/**
	 * @return the id
	 */
	public String getId()
	{
		return mId;
	}

	/**
	 * @return the teamDto
	 */
	public TeamDto getTeamDto()
	{
		mTeamDto.setNom(mNom.getText());
		return mTeamDto;
	}
}
